package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dao.EpidemicDao;
import com.dao.EpidemicSumDao;
import com.dao.SysUserDao;
import com.github.pagehelper.PageHelper;
import com.pojo.Epidemic;
import com.pojo.EpidemicSpecialThings;
import com.pojo.EpidemicSum;
import com.pojo.SysUser;
import com.service.EpidemicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ZhaoJiaDong
 * @version 2020.1
 * @date 2022/11/19 0019 14:41
 */
@Service
public class EpidemicServiceImpl implements EpidemicService {
    @Autowired
    private EpidemicDao epidemicDao;
    @Autowired
    private EpidemicSumDao epidemicSumDao;
    @Autowired
    private SysUserDao sysUserDao;
    @Override
    public boolean add(Epidemic epidemic) {
        int count=epidemicDao.insert(epidemic);
        QueryWrapper<Epidemic> epidemicQueryWrapper = new QueryWrapper<>();
        epidemicQueryWrapper.eq("id",epidemic.getId());
        //查询出单个，遍历更改date数据
        Epidemic epidemic2 = epidemicDao.selectOne(epidemicQueryWrapper);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String date="";
        Integer number0=0;
        Integer number1=0;
        Integer sumnumber=0;
            Date createTime = epidemic2.getCreateTime();
            //格式化
            String newDate=simpleDateFormat.format(createTime);
            //解析
//            java.sql.Date date = java.sql.Date.valueOf(newDate);
            epidemic2.setDate(newDate);
            date=newDate;
            //update
            epidemicDao.updateById(epidemic2);
            if(epidemic2.getType()==0){
                number0++;
            }else{
                number1++;
            }
            sumnumber++;
        //汇总到后台
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("date",date);
        EpidemicSum epidemicSum = epidemicSumDao.selectOne(queryWrapper);
        if(epidemicSum==null){//如果没有记录，就新建一张表
            System.out.println("没有记录，新增表记录");
            EpidemicSum epidemicSum1 = new EpidemicSum();
            epidemicSum1.setNumber0(0+number0);
            epidemicSum1.setNumber1(0+number1);
            epidemicSum1.setSumnumber(0+sumnumber);
            epidemicSum1.setReservation(0);
            epidemicSum1.setDate(date);
            epidemicSumDao.insert(epidemicSum1);
        }else{//如果有记录，在原有记录上添加数据
            System.out.println("有记录，修改记录数据");
            epidemicSum.setNumber0(epidemicSum.getNumber0()+number0);
            epidemicSum.setNumber1(epidemicSum.getNumber1()+number1);
            epidemicSum.setSumnumber(epidemicSum.getSumnumber()+sumnumber);
            epidemicSumDao.updateById(epidemicSum);
        }
        return count<1?false:true;
    }

    @Override
    public List<Epidemic> findByPage(Integer currentPage, Integer pageSize,String username){
        PageHelper.startPage(currentPage,pageSize);
        //创建筛选器
        QueryWrapper<Epidemic> wrapper = new QueryWrapper<>();
        //设置条件
        wrapper.ne("number","0");
        wrapper.ne("uid",username);
        List<Epidemic> list=epidemicDao.selectList(wrapper);
//        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
//        for (Epidemic epidemic : list) {
//            Date createTime = epidemic.getCreateTime();
//            //格式化
//            String newDate=simpleDateFormat.format(createTime);
//            //解析
//            java.sql.Date date = java.sql.Date.valueOf(newDate);
//            epidemic.setCreateTime(date);
//        }
        return list;
    }


    @Override
    public List<Epidemic> findDetails(Integer id){
        //创建筛选器
        QueryWrapper<Epidemic> wrapper = new QueryWrapper<>();
        QueryWrapper<Epidemic> wrapper1 = new QueryWrapper<>();
        //设置条件
        wrapper.eq("id",id);
        //查询出申请人的创建记录时间
        Epidemic epidemic = epidemicDao.selectOne(wrapper);
        Date createTime1 = epidemic.getCreateTime();
        System.out.println("这是查出来的创造时间"+createTime1);
        //设置条件
        wrapper1.eq("create_time",createTime1);
        //查询出同行人和申请人为一组
        List<Epidemic> list = epidemicDao.selectList(wrapper1);
        //转换时间格式
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        for (Epidemic epidemic1 : list) {
            Date createTime = epidemic1.getCreateTime();
            //格式化
            String newDate=simpleDateFormat.format(createTime);
            //解析
            java.sql.Date date = java.sql.Date.valueOf(newDate);
            epidemic1.setCreateTime(date);
        }
        return list;
    }

    @Override
    public boolean deleteById(Integer id) {
         //创建筛选器
        QueryWrapper<Epidemic> wrapper = new QueryWrapper<>();
        QueryWrapper<Epidemic> wrapper1 = new QueryWrapper<>();
        //设置条件
        wrapper.eq("id",id);
        //查询出申请人的创建记录时间
        Epidemic epidemic = epidemicDao.selectOne(wrapper);
        Date createTime1 = epidemic.getCreateTime();
        System.out.println("这是查出来的创造时间"+createTime1);
        //设置条件
        wrapper1.eq("create_time",createTime1);
        //查询出同行人和申请人为一组
        List<Epidemic> list = epidemicDao.selectList(wrapper1);
        //遍历删除
        int count=0;
        for (Epidemic epidemic1 : list) {
            Integer id1 = epidemic1.getId();
            int i=epidemicDao.deleteById(id1);
            count+=i;
        }
        return count<1?false:true;
    }

    @Override
    public int check(String uid) {
        //获取当前时间
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date date=new Date();
        String format = simpleDateFormat.format(date);
        java.sql.Date newDate = java.sql.Date.valueOf(format);
        //检查时间是否在今天晚上8:00——明天早上8:00
        long timeNow = date.getTime();//现在时间
        long time = newDate.getTime();
        System.out.println("00时的毫秒值"+time);
        System.out.println("现在的的毫秒值"+timeNow);
        long timeMin=time+(8*3600*1000);//最早时间
        long timeMax=timeMin+(12*3600*1000);//最晚时间
        System.out.println(timeNow-timeMax+"asdasdsadasdasd");
        System.out.println(timeNow-timeMin+"asdasdsadasdasd");
        if((timeNow-timeMax)<0 && (timeNow-timeMin)>0){

        }else{
            return 1;
        }
        //根据时间和uid查询是否存在
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("uid",uid);
        queryWrapper.eq("reservation",0);
        List list = epidemicDao.selectList(queryWrapper);
        System.out.println(list+"list是这个");
        if(list.size()>=1){
            return 2;
        }else{
            return 0;
        }
    }

    @Override
    public List<EpidemicSum> findSumDataByPage(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<EpidemicSum> list=epidemicSumDao.selectList(null);
        return list;
    }

    @Override
    public List<String> findPhone() {
        QueryWrapper<Epidemic> queryWrapper = new QueryWrapper();
        queryWrapper.eq("reservation",0);
        queryWrapper.select("distinct uid");
        List<Epidemic> list = epidemicDao.selectList(queryWrapper);
        List<String> list1=new ArrayList<>();
        for (Epidemic epidemic : list) {
            String uid = epidemic.getUid();
            QueryWrapper<SysUser> queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("username",uid);
            SysUser sysUser = sysUserDao.selectOne(queryWrapper1);
            String phone = sysUser.getPhone();
            list1.add(phone);
        }
        return list1;
    }

    @Override
    public boolean updateReservation(Integer reservation) {
        QueryWrapper<Epidemic> queryWrapper = new QueryWrapper();
        queryWrapper.eq("reservation",0);
        List<Epidemic> list = epidemicDao.selectList(queryWrapper);
            for(Epidemic epidemic : list) {
                epidemic.setReservation(reservation);
                epidemicDao.updateById(epidemic);
            }
        QueryWrapper<EpidemicSum> queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("reservation",0);
        EpidemicSum epidemicSum = epidemicSumDao.selectOne(queryWrapper1);
        System.out.println(epidemicSum);
//        epidemicSum.setReservation(reservation);
        return false;
    }
}
